<%@ page language="java" contentType="text/html; charset=utf-8"	pageEncoding="utf-8"%>
<html>
<head>


<script type="text/javascript">
var listUrl="rest/roleList.do";

$(document).ready(function () {
//     $("#submit_role-search").click(function () {
//     	$('#role-dg').datagrid('options').pageNumber=1;
//     	$('#role-dg').datagrid('options').pageList = [10, 15, 20, 25 ];
//         $('#role-dg').datagrid({ queryParams: form2Json("fms") });   //点击搜索
//     	$("#role-dg").datagrid('reload');
//     });

    $("#role-dg").datagrid({
        loadMsg: '数据加载中....',
        //title: '角色管理',
//         height:document.body.clientHeight-165,
        pagination: true,
        rownumbers: true,
        width: '100%',
        url: listUrl,
        nowrap: false,
        striped: true,
        sortName: 'id',
        sortOrder: 'asc',
        remoteSort: false,
        rownumbers: true,
        pagination: true,
        pageList : [ 10, 15, 20, 25 ],
        frozenColumns: [
            [
                {field: 'id', checkbox: true},
                {title: '角色编号', field: 'id', width: 0, sortable: true,hidden:'true'}
            ]
        ],
        columns: [
            [
                {field: 'rolename', title: '角色名称', width: 160},
                {field: 'roledesc', title: '角色备注', width: 120}
            ]
        ]
    });// end datagrid
   
    //设置分页控件
    var p = $('#role-dg').datagrid('getPager');
    $(p).pagination({
        pageSize: 10,//每页显示的记录条数，默认为10
        pageList: [10, 15, 20, 25 ],//可以设置每页记录条数的列表
        beforePageText: '第',//页数文本框前显示的汉字
        afterPageText: '页    共 {pages} 页',
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
        onBeforeRefresh: function () {
            $(this).pagination('loading');
            $(this).pagination('loaded');
        }

    });//end pagination




})//end ready

function newRole() {
    var url = "role/add.do";
    $.post(url, {},
    function(data) {
        $("#center").empty();
        $("#center").html(data);
        $.parser.parse($("#center"));
    });
}

function editRole() {
    var rows = $('#role-dg').datagrid('getSelections');
    if (rows.length != 1) {
        $.messager.alert('操作提示', "请先选择一条要编辑的信息！");
        return;
    }

    var row = $('#role-dg').datagrid('getSelected');
    if (row.rolename == 'admin') {
		$.messager.alert('操作提示', "不可以编辑admin角色");
		return;
	}
	
    var id=row.id;

    var url = "role/edit.do";
    $.post(url, row,
    function(data) {
        $("#center").empty();
        $("#center").html(data);
        $.parser.parse($("#center"));
    });

}


function removeRole() {
    var array = $('#role-dg').datagrid('getSelections');
    var ids = new Array();


    for (var i = 0; i < array.length; i++) {//组成一个字符串，ID主键之间用逗号隔开
        if (array[i].rolename == 'admin') {
            $.messager.alert('操作提示', "不能删除admin角色");
            return;
        }
        ids[i] = array[i].id;
    }

    ids = ids.join(',');

    if (array != "") {
        $.messager.defaults = {ok: "确定", cancel: "取消"};
        $.messager.confirm('操作提示', '是否要删除该信息？', function (r) {
            if (r) {
                $.post('rest/roleDel.do',
                    {  ids: ids}, function (response) {

                        if (response.success == "false") {
                            $.messager.alert('操作提示', response.msg, 'error');
                        } else {
                            $('#role-dg').datagrid('reload');
                            $.messager.alert('操作提示', "删除成功", 'info');
                        }
                    });
            }
        });
    } else {
        $.messager.alert('操作提示', "请先选择要删除的信息！");
    }

};

function editMenu() {
	var rows = $('#role-dg').datagrid('getSelections');
    if (rows.length != 1) {
        $.messager.alert('操作提示', "请先选择一个要分配菜单的角色！");
        return;
    }
    
    

    var row = $('#role-dg').datagrid('getSelected');
    if (row.rolename == 'admin') {
		$.messager.alert('操作提示', "admin不需要分配菜单");
		return;
	}
    var url = "rolemenu/index.do";
    $.post(url, ("roleId=" + row.id),
    function(data) {
        $("#center").empty();
        $("#center").html(data);
        $.parser.parse($("#center"));
    });
}

/**
 * 分配权限操作
 */
function editOperation() {
	var rows = $('#role-dg').datagrid('getSelections');
    if (rows.length != 1) {
        $.messager.alert('操作提示', "请先选择一个要分配权限的角色！");
        return;
    }
    
    
    var row = $('#role-dg').datagrid('getSelected');
    if (row.rolename == 'admin') {
		$.messager.alert('操作提示', "admin已经是超级管理员权限");
		return;
	}
    var url = "roleoperation/index.do";
    $.post(url, ("roleId=" + row.id),
    function(data) {
        $("#center").empty();
        $("#center").html(data);
        $.parser.parse($("#center"));
    });
}
</script>
</head>
<body>


		<!-- <div class="breadcrumb">系统 &gt;&gt; 角色管理</div> -->
		<div class="linkbar"><a>系统</a>/<a>角色管理</a></div>
		
		<div class="clear"></div>
		<div id="role-dg" toolbar="#toolbar"></div>

		<div id="toolbar">
			<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true"	onclick="newRole()">添加角色</a>
			<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editRole()">编辑角色</a>
			<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="removeRole()">删除角色</a>
			<a href="#" class="easyui-linkbutton" iconCls="icon-edit" 	plain="true" onclick="editMenu()">分配菜单</a>
<!-- 			<shiro:hasPermission name="role:editOperation">  -->
<!-- 			<a href="#" class="easyui-linkbutton" iconCls="icon-edit" 	plain="true" onclick="editOperation()">分配操作权限</a> -->
<!-- 			</shiro:hasPermission> -->
		</div>


</body>
</html>